package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.HotProjectBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.HotProjectPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.HotProjectAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IHotProjectView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/2.
 * Desc   : 热门项目
 */

public class HotProjectActivity extends BaseActivity<HotProjectPresenter> implements IHotProjectView {

    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.recyclerview_hotproject)
    RecyclerView recyclerviewHotproject;
    @BindView(R.id.view_bottomline)
    View viewBottomline;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;

    private PageRecorder recorder = new PageRecorder();
    private HotProjectAdapter hotProjectAdapter;

    public static Intent createIntent(Context context){
        return new Intent(context,HotProjectActivity.class);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_hot_project);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new HotProjectPresenter(this);
    }

    @Override
    protected void init() {
        mPresenter.getList(recorder.getCurrentPage());
        smartRefresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                recorder.moveToFirstPage();
                mPresenter.getList(recorder.getFirstPage());
            }

            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.addList(recorder.getCurrentPage());
            }
        });
        setTopView();
        List<HotProjectBean> list = new ArrayList<>();
        recyclerviewHotproject.setLayoutManager(new LinearLayoutManager(mContext));
        hotProjectAdapter = new HotProjectAdapter(mContext, R.layout.item_hot_project, list);
        recyclerviewHotproject.setAdapter(hotProjectAdapter);
        hotProjectAdapter.setmOnItemClickListener((adapter, position) ->
                startActivity(HotProjectDetailActivity.createIntent(this, hotProjectAdapter.getDatas().get(position).getId())));
    }

    private void setTopView() {
        imgLeft.setImageResource(R.mipmap.icon_left);
        txtTitle.setText("热门项目");
        viewBottomline.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.ll_leftview)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setData(List<HotProjectBean> data) {
        hotProjectAdapter.setDatas(data);
        recorder.moveToNextPage();
        smartRefresh.finishRefresh();
    }

    @Override
    public void addData(List<HotProjectBean> data) {
        hotProjectAdapter.addDatas(data);
        recorder.moveToNextPage();
        smartRefresh.finishLoadmore();
    }
}
